# -*- coding:utf-8 -*-

"""
DESC:
auth:lt
date:2024-03-11
"""

import requests

# 公共变量
API_HOST = "https://admin-nodeai-api.skysrt.com/node-ai-admin-api/flowsadmin"
# API_HOST = "http://127.0.0.1:6489/flowsadmin"
# API_KEY = "abc123abc"
API_KEY = "0M8x0201"


# 删除容器
def delete_statefulset(group_name, namespace):
    api_url = f"{API_HOST}/deK8sPod?groupName={group_name}&namespace={namespace}"
    headers = {
        "Content-Type": "application/json",
        "X-Api-Key": API_KEY
    }
    print(api_url)
    try:
        response = requests.get(api_url, headers=headers)
        print(response.text)
    except requests.exceptions.RequestException as e:
        print(f"An error occurred while deleting StatefulSet {group_name}: {e}")


# 获取要删除路由的信息
def get_group_names_and_router_ids():
    api_url = f"{API_HOST}/getNodeAiBetaRouter"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        group_names_and_ids = []
        for item in data['data']:
            group_names_and_ids.append((item['groupName'], item['id']))
        return group_names_and_ids
    else:
        print("Error:", response.text)
        return []


# 获取要删除路由的信息
def get_group_names_and_upstream_ids():
    api_url = f"{API_HOST}/getNodeAiBetaUpstream"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        group_names_and_ids = []
        for item in data['data']:
            # 检查'groupName'键是否存在
            if 'groupName' in item:
                groupName = item['groupName']
                print(groupName)
                group_names_and_ids.append((groupName, item['id']))
            else:
                print("groupName not found for item, skipping.")
        return group_names_and_ids
    else:
        print("Error:", response.text)
        return []


def get_group_names_pod_list(namespace):
    api_url = f"{API_HOST}/getPodList?namespace={namespace}"
    response = requests.get(api_url)
    if response.status_code == 200:
        data = response.json()
        group_names_set = set('-'.join(name.split('-')[:-1]) for name in data['data'])
        group_names = list(group_names_set)
        return group_names
    else:
        print("Error:", response.text)
        return []


def delete_route(route_id):
    api_url = f"{API_HOST}/deRoute?id={route_id}"
    print(api_url)
    response = requests.get(api_url)
    print(response.text)


# Step 1: Get group names and IDs from A interface
# rt_group_names_and_router_ids = get_group_names_and_router_ids()
# print(rt_group_names_and_router_ids)

# ups = get_group_names_and_upstream_ids()
# print(ups)

pods = get_group_names_pod_list("nodeai-beta")
for groupName in pods:
    delete_statefulset(groupName, "nodeai-beta")

pods = get_group_names_pod_list("nodeai-development")
for groupName in pods:
    delete_statefulset(groupName, "nodeai-development")

pods = get_group_names_pod_list("nodeai-production")
for groupName in pods:
    delete_statefulset(groupName, "nodeai-production")

# delete_statefulset("nodeai-production-serv-36-97", "nodeai-production")

# Step 2 and 3: Delete statefulsets and routes for each group name and ID
# for groupName, id in ups:
#     delete_statefulset(groupName, "nodeai-development")
#     delete_route(id)
